import { uniPostcssPlugin } from '@dcloudio/uni-cli-shared'
import postcssImport from 'postcss-import'
import tailwindcss from 'tailwindcss'
// import postcssClassRename from 'postcss-class-rename'
import cssByebye from 'css-byebye'
import autoprefixer from 'autoprefixer'
import path from 'path'
import postcssPxtorpxPro from 'postcss-pxtorpx-pro'

const uniInputDir: string = process.env.UNI_INPUT_DIR as string

export default {
    plugins: [
        postcssImport({
            resolve(id) {
                if (id.startsWith('~@/')) {
                    return path.resolve(uniInputDir, id.substr(3))
                } else if (id.startsWith('@/')) {
                    return path.resolve(uniInputDir, id.substr(2))
                } else if (id.startsWith('/') && !id.startsWith('//')) {
                    return path.resolve(uniInputDir, id.substr(1))
                }
                return id
            }
        }),
        tailwindcss(),

        // 根据平台差异进行不同的样式处理
        ...(process.env.UNI_PLATFORM !== 'h5'
            ? [
                // 使用postcss-class-name 包将小程序不支持的类名转换为支持的类名
                // postcssClassRename({
                //     '\\\\:': '-css-',
                //     '\\\\/': '-css-',
                //     '\\\\.': '-css-',
                //     // '.:': '--',
                //     '\\*': '-css-'
                // }),
                cssByebye({
                    rulesToRemove: [/\*/],
                    map: false
                })
            ]
            : []),
        uniPostcssPlugin(),
        autoprefixer({
            remove: true
        }),
        postcssPxtorpxPro()
    ]
}